package com.fjec.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.exception.BizException;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.shiro.AccountInfo;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.SequenceIdGenerator;
import com.fjec.common.view.AnnexBizItem;
import com.fjec.market.entity.BizRating;
import com.fjec.market.entity.BizSubPhysicalOrder;
import com.fjec.market.entity.InfoSku;
import com.fjec.market.feign.BaseClient;
import com.fjec.market.mapper.BizRatingMapper;
import com.fjec.market.mapper.InfoSkuMapper;
import com.fjec.market.service.IBizRatingService;
import com.fjec.market.service.IBizSubPhysicalOrderService;
import com.fjec.market.service.IInfoSkuService;
import org.elasticsearch.common.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 013.商品评价表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-09-22
 */
@Service
public class BizRatingServiceImpl extends ServiceImpl<BizRatingMapper, BizRating> implements IBizRatingService {

    @Resource
    private BizRatingMapper mapper;

    @Resource
    private BaseClient baseClient;

    @Resource
    private InfoSkuMapper infoSkuMapper;

    @Resource
    private IBizSubPhysicalOrderService bizSubPhysicalOrderService;

    @Resource
    private SequenceIdGenerator idGenerator;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public JsonResult subOrderRating(Long id, Integer qualityRating, Integer speedRating, Integer logisticsRating,
                                     String skuComment, String picIds, String picUrls) {
        // 多次评价校验
        QueryWrapper<BizRating> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("OrderId", id)
                .eq("DelFlag", 0);
        long count = mapper.selectCount(queryWrapper);
        if (count > 0) {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_3010.getCode())
                    .message(EnumSysCode.ERR_3010.getMsg())
                    .build();
        }
        // check子订单是否存在
        BizSubPhysicalOrder subOrder = bizSubPhysicalOrderService.getById(id);
        if (subOrder == null || subOrder.getDelFlag() == 1) {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0041.getCode())
                    .message(EnumSysCode.ERR_0041.getMsg())
                    .build();
        }
        // check订单状态是否为已完成
        if (subOrder.getOrderStatus() != 4) {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0042.getCode())
                    .message(EnumSysCode.ERR_0042.getMsg())
                    .build();
        }
        InfoSku sku = infoSkuMapper.selectById(subOrder.getSkuId());
        Long ratingId = idGenerator.nextId();
        BizRating entity = new BizRating();
        entity.setId(ratingId);
        entity.setOrderId(subOrder.getOrderId());
        entity.setSubOrderId(subOrder.getId());
        entity.setUserId(AccountInfo.getUserId());
        entity.setBuyerType(AccountInfo.getBusiType());
        entity.setBusiType(sku.getBusiType());
        entity.setShopId(sku.getBusiId());
        entity.setSpuId(sku.getSpuId());
        entity.setQualityRating(qualityRating);
        entity.setSpeedRating(speedRating);
        entity.setLogisticsRating(logisticsRating);
        entity.setSkuComment(skuComment);
        entity.setDelFlag(0);
        entity.setCreateTime(DateUtil.nowLdt());
        // 保存照片数组
        if (!Strings.isNullOrEmpty(picUrls)) {
            List<String> picUrlList = Arrays.asList(picUrls.split(","));
            entity.setImages(picUrlList);
        }
        mapper.insert(entity);
        // 评价照片有效处理
        if (!Strings.isNullOrEmpty(picIds)) {
            List<AnnexBizItem> itemList = new ArrayList<>();
            List<String> picIdList = Arrays.asList(picIds.split(","));
            picIdList.forEach((String picId) ->{
                AnnexBizItem item = new AnnexBizItem();
                item.setAnnexId(Long.valueOf(picId));
                item.setBizType(11);
                item.setBizId(ratingId);
                itemList.add(item);
            });
            int ret = baseClient.enableAnnexStatus(itemList);
            if (ret == -1) {
                throw new BizException(EnumSysCode.ERR_3001.getCode(), EnumSysCode.ERR_3001.getMsg());
            }
        }
        // todo sth
        return JsonResult.success()
                .code(EnumSysCode.MSG_6002.getCode())
                .message(EnumSysCode.MSG_6002.getMsg())
                .build();
    }

    @Override
    public List<Map<String, Object>> getPageBySpuId(Long spuId, Page page) {
        return mapper.getPageBySpuId(spuId, page);
    }

    @Override
    public List<Map<String, Object>> getSpuStarForSchedule() {
        return mapper.getSpuStarForSchedule();
    }
}
